package VIEWS;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JToolBar;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import com.toedter.calendar.JCalendar;

import CONTROLLERS.BenhNhanControllers;
import CONTROLLERS.UsersControllers;
import ModelEntityFW.BenhNhanEntity;
import ModelEntityFW.UserEntitys;
import Ultils.DateConverter;

import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.Date;
import java.awt.event.ActionEvent;
import com.toedter.calendar.JDateChooser;

public class BenhNhanAdd extends JFrame {

	private JPanel contentPane;
	private JTextField textBNID;
	private JTextField textHoVaTen;
	private JTextField textDiaChi;
	private JTextField textDienThoai;
	private JTextField textQueQuan;
	private JTextField textCMND;
	private JDateChooser Birth;
	private static boolean bFlag = false;

	private static Connection m_objConnection;
	private static UserEntitys m_objSecurityContext;
	private static BenhNhanEntity m_objBenhNhanEntity;
	// private static UserEntitys m_objUserEntity;
	private BenhNhanControllers m_objBenhNhanControllers;

	/**
	 * Launch the application.
	 */
	/*
	 * public static void main(String[] args) { EventQueue.invokeLater(new
	 * Runnable() { public void run() { try { BenhNhanAdd frame = new
	 * BenhNhanAdd(m_objConnection, m_objSecurityContext);
	 * frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }
	 * }); }
	 * 
	 * /** Create the frame.
	 */
	public BenhNhanAdd(Connection objConnection, UserEntitys objSecurity, BenhNhanEntity objBenhNhan) {
		m_objConnection = objConnection;
		m_objSecurityContext = objSecurity;
		m_objBenhNhanEntity = new BenhNhanEntity();
		m_objBenhNhanEntity = objBenhNhan;
		m_objBenhNhanControllers = new BenhNhanControllers(m_objConnection, m_objSecurityContext);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 385);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JToolBar toolBar = new JToolBar();
		toolBar.setBounds(0, 0, 434, 38);
		contentPane.add(toolBar);

		JButton btnNewButton = new JButton("NewButton");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				m_objBenhNhanEntity.setHoVaTen(textHoVaTen.getText());
				m_objBenhNhanEntity.setDiaChi(textDiaChi.getText());
				m_objBenhNhanEntity.setDienThoai(textDienThoai.getText());
				m_objBenhNhanEntity.setNgaySinh(DateConverter.convertDateToString(Birth.getDate(),"yyyy-MM-dd"));
				m_objBenhNhanEntity.setQueQuan(textQueQuan.getText());
				m_objBenhNhanEntity.setCMND(textCMND.getText());



				if (m_objBenhNhanEntity.isbFlag()) {
					m_objBenhNhanEntity.setBN_ID(Integer.parseInt(textBNID.getText()));
					boolean bResult = false;
					try {
						bResult = m_objBenhNhanControllers.ExecOfUpdateElement(m_objBenhNhanEntity);
					} catch (Exception e) {
						JOptionPane.showMessageDialog(getParent(), "Err: " + e, "Thông báo",
								JOptionPane.INFORMATION_MESSAGE);
					}
					if (bResult) {
						JOptionPane.showMessageDialog(getParent(), "Cập nhật thông tin bệnh nhân thành công!",
								"Thông báo", JOptionPane.INFORMATION_MESSAGE);

						dispose();
						m_objBenhNhanEntity.setbFlag(bFlag);
					}
				} else {
					boolean bResult = false;
					try {
						bResult = m_objBenhNhanControllers.ExecOfInsertElement(m_objBenhNhanEntity);
					} catch (Exception e) {
						JOptionPane.showMessageDialog(getParent(), "Err: " + e, "Thông báo",
								JOptionPane.INFORMATION_MESSAGE);
					}
					if (bResult) {
						JOptionPane.showMessageDialog(getParent(), "Thêm bệnh nhân thành công!", "Thông báo",
								JOptionPane.INFORMATION_MESSAGE);

						// Giải phóng form
						dispose();
					}
				}

			}
		});
		btnNewButton.setIcon(new ImageIcon(BenhNhanAdd.class.getResource("/images/book_edit.png")));
		btnNewButton.setFont(new Font("Cambria", Font.PLAIN, 14));
		toolBar.add(btnNewButton);

		JButton btnng = new JButton("Đóng");
		btnng.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnng.setIcon(new ImageIcon(BenhNhanAdd.class.getResource("/images/cancel.gif")));
		btnng.setFont(new Font("Cambria", Font.PLAIN, 14));
		toolBar.add(btnng);

		JLabel lblNewLabel = new JLabel("BN_ID:");
		lblNewLabel.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNewLabel.setBounds(10, 61, 76, 26);
		contentPane.add(lblNewLabel);

		JLabel lblHVTn = new JLabel("Họ Và Tên:");
		lblHVTn.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblHVTn.setBounds(10, 98, 76, 26);
		contentPane.add(lblHVTn);

		JLabel lblaCh = new JLabel("Địa Chỉ:");
		lblaCh.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblaCh.setBounds(10, 135, 76, 26);
		contentPane.add(lblaCh);

		JLabel lblinThoi = new JLabel("Điện Thoại:");
		lblinThoi.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblinThoi.setBounds(10, 172, 76, 26);
		contentPane.add(lblinThoi);

		JLabel lblNgySinh = new JLabel("Ngày Sinh:");
		lblNgySinh.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNgySinh.setBounds(10, 209, 76, 26);
		contentPane.add(lblNgySinh);

		JLabel lblQuQun = new JLabel("Quê Quán:");
		lblQuQun.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblQuQun.setBounds(10, 246, 76, 26);
		contentPane.add(lblQuQun);

		JLabel lblCmnd = new JLabel("CMND:");
		lblCmnd.setToolTipText("");
		lblCmnd.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblCmnd.setBounds(10, 283, 76, 26);
		contentPane.add(lblCmnd);

		textBNID = new JTextField();
		textBNID.setEditable(false);
		textBNID.setFont(new Font("Cambria", Font.PLAIN, 14));
		textBNID.setBounds(96, 61, 101, 26);
		contentPane.add(textBNID);
		textBNID.setColumns(10);

		textHoVaTen = new JTextField();
		textHoVaTen.setFont(new Font("Cambria", Font.PLAIN, 14));
		textHoVaTen.setColumns(10);
		textHoVaTen.setBounds(96, 98, 315, 26);
		contentPane.add(textHoVaTen);

		textDiaChi = new JTextField();
		textDiaChi.setFont(new Font("Cambria", Font.PLAIN, 14));
		textDiaChi.setColumns(10);
		textDiaChi.setBounds(96, 135, 315, 26);
		contentPane.add(textDiaChi);

		textDienThoai = new JTextField();
		textDienThoai.setFont(new Font("Cambria", Font.PLAIN, 14));
		textDienThoai.setColumns(10);
		textDienThoai.setBounds(96, 172, 315, 26);
		contentPane.add(textDienThoai);

		textQueQuan = new JTextField();
		textQueQuan.setFont(new Font("Cambria", Font.PLAIN, 14));
		textQueQuan.setColumns(10);
		textQueQuan.setBounds(96, 246, 315, 26);
		contentPane.add(textQueQuan);

		textCMND = new JTextField();
		textCMND.setFont(new Font("Cambria", Font.PLAIN, 14));
		textCMND.setColumns(10);
		textCMND.setBounds(96, 283, 315, 26);
		contentPane.add(textCMND);
		
	    Birth = new JDateChooser();
		Birth.setDateFormatString("dd/MM/yyyy");
		Birth.setBounds(96, 209, 158, 26);
		contentPane.add(Birth);
		
        if (m_objBenhNhanEntity.isbFlag()) {
            // Khi cập nhật
        	setTitle("---Cập nhật bệnh nhân---");
        	btnNewButton.setText("Cập nhật");
            BindingControls(m_objBenhNhanEntity);
        }else{
        	setTitle("---Thêm bệnh nhân---");
        	btnNewButton.setText("Thêm mới");
        }
        
        
		if (m_objBenhNhanEntity.isbFlag()) {

			BindingControls(m_objBenhNhanEntity);
		}
	}

	void BindingControls(BenhNhanEntity objBenhNhanEntity) {
		textBNID.setText("" + objBenhNhanEntity.getBN_ID());
		textHoVaTen.setText(objBenhNhanEntity.getHoVaTen());
		textDiaChi.setText(objBenhNhanEntity.getDiaChi());
		textDienThoai.setText(objBenhNhanEntity.getDienThoai());
		Birth.setDate(DateConverter.convertStringToDate(objBenhNhanEntity.getNgaySinh(),"yyyy-MM-dd"));
		textQueQuan.setText(objBenhNhanEntity.getQueQuan());
		textCMND.setText(objBenhNhanEntity.getCMND());

	}
}
